//
//  ActListViewController.m
//  Tiger
//
//  Created by  on 12-8-6.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "ActListViewController.h"
#import "ASIHTTPRequest.h"
#import "TigerAppDelegate.h"

@interface ActListViewController ()

@end
static float      p_valuearr[8] ={
  0.06f,-0.045f,0.033f,-0.023f,0.015f,-0.009f,0.005f,0.0f
};
@implementation ActListViewController

-(id) init
{
    if (self = [super init]) {
        p_UnSeeCount =[[NSMutableArray alloc] init];
        p_UsedCount=[[NSMutableArray alloc] init];
    }
    return self;
}

-(void) dealloc
{
    [p_ListRequest setDelegate:nil];
    [p_ListRequest release];
    [p_UnSeeCount release];
    [p_UsedCount release];
    [p_ActListArr release];
    [super dealloc];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    UITableView* tListTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 480-88) style:UITableViewStylePlain];
    tListTableView.delegate = self;
    tListTableView.dataSource = self;
    tListTableView.tag =0x200;
    [self.view addSubview:tListTableView];
    [tListTableView release];
    

    p_HaveTwo= NO;
    p_ListRequest = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://www.speedtiger.com.cn/ios3/act_list.html"]];
    [p_ListRequest setDelegate:self];
    [p_ListRequest startAsynchronous];
    [p_ListRequest retain];
    
    m_popView = [[UIControl alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height)];
    [m_popView setBackgroundColor:[UIColor clearColor]];
    [m_popView addTarget:self action:@selector(chagebutton) forControlEvents:UIControlEventTouchUpInside];
    UIImageView* popimageview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 410)];
    [popimageview setImage:[UIImage imageNamed:@"pop.png"]];
    [m_popView addSubview:popimageview];
    [popimageview release];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

#pragma mark ASIHTTPRequestDelegate methods
- (void)requestFailed:(ASIHTTPRequest *)request
{
	[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
	UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"无可用的网络连接" delegate:self 
										   cancelButtonTitle:nil otherButtonTitles:nil] autorelease];
	[alert addButtonWithTitle:@"确定"];
	[alert show];
	
}
- (void)requestFinished:(ASIHTTPRequest *)request
{
    NSLog(@"%@",[request responseString]);
	NSData *responseData = [request responseData];
	NSString *responseStr = [[[NSString alloc] initWithData:responseData encoding: NSUTF8StringEncoding] autorelease];

    NSDictionary *tdict = [responseStr JSONValue];
    p_ActListArr = [tdict objectForKey:@"items"];
    [p_ActListArr retain];
    p_HaveTwo = NO;
    for (NSDictionary* tDic in p_ActListArr) {
        int tPass =[[tDic objectForKey:@"isPass"] intValue];
        if (tPass == 0) {
            [p_UnSeeCount addObject:tDic];
        }else {
           [p_UsedCount addObject:tDic]; 
        }
    }
    if ([p_UnSeeCount count] > 0 && [p_UsedCount count] > 0) {
        p_HaveTwo =YES;
    }
    UITableView* tInTableView = (UITableView*)[self.view viewWithTag:0x200];
    [tInTableView reloadData];
}

-(void) popAnimate
{
    NSLog(@"%f",p_AnimateValue);//p_valuearr
    p_AnimateValue = p_valuearr[p_AnimateVCut];
    p_AnimateVCut++;
    if (p_AnimateVCut > 7) {
        return;
    }
    CGAffineTransform tTargTrans = CGAffineTransformMakeRotation(p_AnimateValue);
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
    [UIView setAnimationDidStopSelector:@selector(popAnimate)];
    m_popView.transform =tTargTrans;
    [UIView commitAnimations];
}

#pragma mark-
#pragma mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (!m_popView.superview) {
        TigerAppDelegate* tApp = (TigerAppDelegate*)[UIApplication sharedApplication].delegate;
        [tApp.window addSubview:m_popView];
        p_AnimateVCut = 0;
        [self popAnimate];
    }
}

#pragma mark-
#pragma mark UITableViewDataSource
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (p_HaveTwo) {
        if (section == 0) {
            return [NSString stringWithFormat:@"新的或未使用的优惠(%d)",[p_UnSeeCount count]];
        }else {
            return [NSString stringWithFormat:@"已使用或已过期(%d)",[p_UsedCount count]];
        }
    }else {
        if ([p_UnSeeCount count] > 0) {
            return [NSString stringWithFormat:@"新的或未使用的优惠(%d)",[p_UnSeeCount count]];
        }
        if ([p_UsedCount count] > 0) {
            return [NSString stringWithFormat:@"已使用或已过期(%d)",[p_UsedCount count]];
        }
    }
    return @"";
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (p_HaveTwo) {
        return 2;
    }
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (p_HaveTwo) {
        if (section == 0) {
            return [p_UnSeeCount count];
        }else {
            return [p_UsedCount count];
        }
    }else {
        if ([p_UnSeeCount count] > 0) {
            return [p_UnSeeCount count];
        }
        if ([p_UsedCount count] > 0) {
            return [p_UsedCount count];
        }
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    int section = [indexPath section];
    int row = [indexPath row];
    static NSString* s_CellContact =@"cellcontact";
    UITableViewCell* tCell = [tableView dequeueReusableCellWithIdentifier:s_CellContact];
    if (tCell == nil) {
        tCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:s_CellContact];
        tCell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    NSDictionary* tRowDic = nil;
    [tCell.textLabel setTextColor:[UIColor blackColor]];
    if (p_HaveTwo) {
        if (section == 0) {
            tRowDic = [p_UnSeeCount objectAtIndex:row];
        }else {
            [tCell.textLabel setTextColor:[UIColor grayColor]];
            tRowDic = [p_UsedCount objectAtIndex:row];
        }
    }else {
        if ([p_UnSeeCount count] > 0) {
            tRowDic = [p_UnSeeCount objectAtIndex:row];
        }
        if ([p_UsedCount count] > 0) {
            [tCell.textLabel setTextColor:[UIColor grayColor]];
            tRowDic = [p_UsedCount objectAtIndex:row];
        }
    }
    tCell.textLabel.text =[tRowDic objectForKey:@"title"];
    return tCell;
}



- (void)chagebutton
{
    if (m_popView.superview) {
        [m_popView removeFromSuperview];
    }
}
@end
